import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

/**
 * User: James Barker
 * Date: 1/08/11
 */
public class ElevatorSimulatorJava extends JFrame implements ActionListener {

    AdminUI aui;
    FloorUI fui;
    ElevatorUI eui;
    ElevatorController ec;

    public ElevatorSimulatorJava() {
        this.setLayout(new FlowLayout());
        this.setTitle("AdminUI");
        this.setSize(300, 300);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        createAdminUI();
        createFloorUI();
        createElevatorUI();

        displayAdminUI();
        this.setVisible(true);
    }

    public void createAdminUI() {
        aui = new AdminUI();
        aui.startButton.addActionListener(this);
        this.add(aui);
        aui.setVisible(false);
    }

    public void displayAdminUI() {
        hideAllUI();
        aui.setVisible(true);
    }

    public void createFloorUI() {
        fui = new FloorUI(1);
        fui.callLift.addActionListener(this);
        this.add(fui);
        fui.setVisible(false);
    }

    public void displayFloorUI() {
        hideAllUI();
        //fui.setCurrentFloor(ec.e.getCurrentFloor());
        fui.setVisible(true);
    }

    public void createElevatorUI() {
        eui = new ElevatorUI();
        //eui.callLift.addActionListener(this);
        this.add(eui);
        eui.setVisible(false);
    }

    public void displayElevatorUI() {
        hideAllUI();
        eui.setVisible(true);
    }


    public void hideAllUI() {
        aui.setVisible(false);
        fui.setVisible(false);
        eui.setVisible(false);
    }









    public void actionPerformed(ActionEvent ae) {
        if (ae.getActionCommand().equals("start")) {
            ec = new ElevatorController(aui.getMinFloor(), aui.getMaxFloor());
            eui.setNoFloorButtons(aui.getMinFloor(), aui.getMaxFloor());
            displayFloorUI();
        }

        if (ae.getActionCommand().equals("call")) {
            System.out.println("CALLING FROM " + fui.getCurrentFloor());
            ec.moveElevator(fui.getCurrentFloor());
            fui.callLift.setText("enter");
        }

        if (ae.getActionCommand().equals("enter")) {
            System.out.println("ENTERING LIFT");
            displayElevatorUI();

            //eui.jb3.addActionListener(this);

            this.add(eui);
        }

        if (ae.getActionCommand().equals("1")) {
            System.out.println("pressed 1");
            ec.moveElevator(1);
            eui.setVisible(false);
            displayFloorUI();
        }

        if (ae.getActionCommand().equals("2")) {
            System.out.println("pressed 2");
            ec.moveElevator(2);
            eui.setVisible(false);
            displayFloorUI();
        }

        if (ae.getActionCommand().equals("3")) {
            System.out.println("pressed 3");
            ec.moveElevator(3);
            eui.setVisible(false);
            displayFloorUI();
        }
    }

    public static void main(String[] args) {
        new ElevatorSimulatorJava();
    }
}
